const { ipcRenderer, remote } = require('electron')
let interval, submitting



if (!interval) {
    let count = 0
    interval = setInterval(() => {
        console.log("loop")
        try {

            let err = document.getElementsByClassName("hal-h0 hal-width-xl")
            if (err.length > 0) {
                require("electron").ipcRenderer.sendTo(remote.getCurrentWindow().getParentWindow().id, "blocked")
            }


            let pageData = ipcRenderer.sendSync("get-page", remote.getCurrentWindow().id)
            if (pageData.method && !submitting) {
                func[pageData.method](pageData.args)
            }
        }
        catch (e) {
            require("electron").ipcRenderer.sendTo(remote.getCurrentWindow().getParentWindow().id, "error", e.message)
        }
    }, 1000)
}
window.onload = () => {
    submitting = false
}

let checkPlace = (from, fromName, to, toName, chhm_from, chhm_to, date) => {
    let table = document.getElementById("schedules_interactive_f")

    if (table) {
        let fromObj = document.getElementById("schedules_interactive_f:hl21-standardLocation_businessLocode")
        let toObj = document.getElementById("schedules_interactive_f:hl56-standardLocation_businessLocode")

        let fromObjName = document.getElementById("schedules_interactive_f:hl21-location_businessLocationName")
        let toObjName = document.getElementById("schedules_interactive_f:hl56-location_businessLocationName")

        //地点不正确重新提交
        if ((fromObj.value == from || fromObjName.value == fromName) && (toObj.value == to || toObjName.value == toName)) {
            return true
        }
        else {
            document.getElementById("schedules_interactive_f:hl21-location_businessLocationName").value = fromName ? fromName : from
            document.getElementById("schedules_interactive_f:hl21-standardLocation_businessLocode").value = from
            document.getElementById("schedules_interactive_f:hl21").value = from
            document.getElementById("schedules_interactive_f:hl56-location_businessLocationName").value = toName ? toName : to
            document.getElementById("schedules_interactive_f:hl56-standardLocation_businessLocode").value = to
            document.getElementById("schedules_interactive_f:hl56").value = to
            document.getElementById("schedules_interactive_f:hl29").value = date

            document.getElementById("schedules_interactive_f:hl33").value = "6"

            document.getElementsByName("schedules_interactive_f:hl95")[chhm_from].checked = true
            document.getElementsByName("schedules_interactive_f:hl101")[chhm_to].checked = true
            submitting = true
            document.getElementById("schedules_interactive_f:hl105").click()
        }
    }

    return false
}

let checkRoute = (index) => {
    let deps = document.getElementsByName("hl124")
    if (deps[index].checked) {
        return true
    }
    else {
        submitting = true
        deps[index].checked = true
        document.getElementById("schedules_interactive_f:hl124:hl153").click()
    }

    return false
}

let checkDetail = (index, args) => {
    let details = document.getElementsByName("hl164")
    if (details[index].checked) {
        if (document.getElementById("schedules_interactive_f:hl202")) {
            ipcRenderer.sendSync("save-page", {
                id: remote.getCurrentWindow().id,
                data: {
                    method: undefined
                }
            })
            require("electron").ipcRenderer.sendTo(remote.getCurrentWindow().getParentWindow().id, "get-vessel", {
                index: args.index,
                route_index: args.route_index,
                detail_index: args.detail_index,
                list: []
            })
        }
        return true
    }
    else {
        submitting = true
        details[index].checked = true
        args.backUrl = location.href
        ipcRenderer.sendSync("save-page", {
            id: remote.getCurrentWindow().id,
            data: {
                method: "getVessel",
                args: args
            }
        })
        document.getElementById("schedules_interactive_f:hl164:hl192").click()
    }

    return false
}


let func = {
    getRoute(args) {
        console.log("get route loop")

        if (!checkPlace(args.from, args.fromName, args.to, args.toName, args.fromCH, args.toCH, args.date)) {
            return
        }

        let list = []
        let listTable = document.getElementById("schedules_interactive_f:hl124")

        if (listTable) {
            if (!window.jQuery) {
                window.jQuery = window.$ = require("jquery")
            }
            $(listTable).find("tbody tr").each((index, item) => {
                let tr = []
                $(item).find("td").each((index, item) => {
                    let html = $(item).html()
                    if (html) {
                        tr.push(html)
                    }
                })
                list.push(tr)
            })
        }

        ipcRenderer.sendSync("save-page", {
            id: remote.getCurrentWindow().id,
            data: {
                method: undefined
            }
        })


        require("electron").ipcRenderer.sendTo(remote.getCurrentWindow().getParentWindow().id, "get-route", {
            index: args.index,
            list
        })
    },
    getDetail(args) {
        console.log("get detail loop")
        if (!checkPlace(args.from, args.fromName, args.to, args.toName, args.fromCH, args.toCH, args.date)) {
            return
        }

        if (!checkRoute(args.route_index)) {
            return
        }

        let list = []
        let form = document.getElementById("schedules_interactive_f:hl164")
        if (form) {
            if (!window.jQuery) {
                window.jQuery = window.$ = require("jquery")
            }
            $(form).find("tbody tr").each((index, item) => {
                let tr = []
                $(item).find("td").each((index, item) => {
                    let text = $(item).text()
                    tr.push(text.trim())
                })
                list.push(tr)
            })
        }

        ipcRenderer.sendSync("save-page", {
            id: remote.getCurrentWindow().id,
            data: {
                method: undefined
            }
        })
        require("electron").ipcRenderer.sendTo(remote.getCurrentWindow().getParentWindow().id, "get-detail", {
            index: args.index,
            route_index: args.route_index,
            list
        })
    },
    getVessel(args) {
        console.log("get vessel loop")
        let fclForm = document.getElementById("schedules_interactive_f:hl234")
        if (fclForm) {
            ipcRenderer.sendSync("save-page", {
                id: remote.getCurrentWindow().id,
                data: {
                    method: undefined
                }
            })
            if (!window.jQuery) {
                window.jQuery = window.$ = require("jquery")
            }
            require("electron").ipcRenderer.sendTo(remote.getCurrentWindow().getParentWindow().id, "get-vessel", {
                index: args.index,
                route_index: args.route_index,
                detail_index: args.detail_index,
                fcl: $(fclForm).find("span:eq(1)").text() + " " +
                    $(document.getElementById("schedules_interactive_f:hl260")).find("span:eq(1)").text()
            })
            submitting = true
            location.href = args.backUrl
            return
        }
        else {
            let detailButton = document.getElementById("schedules_interactive_f:hl40:hl54")
            if (detailButton) {
                document.getElementsByName("hl40")[0].checked = true
                submitting = true
                detailButton.click()
                return
            }
        }




        if (!checkPlace(args.from, args.fromName, args.to, args.toName, args.fromCH, args.toCH, args.date)) {
            return
        }

        if (!checkRoute(args.route_index)) {
            return
        }

        if (!checkDetail(args.detail_index, args)) {
            return
        }

    }
}

ipcRenderer.on("get-route", (e, args) => {
    ipcRenderer.sendSync("save-page", {
        id: remote.getCurrentWindow().id,
        data: {
            method: "getRoute",
            args
        }
    })
})

ipcRenderer.on("get-detail", (e, args) => {
    ipcRenderer.sendSync("save-page", {
        id: remote.getCurrentWindow().id,
        data: {
            method: "getDetail",
            args
        }
    })
})

ipcRenderer.on("get-vessel", (e, args) => {
    ipcRenderer.sendSync("save-page", {
        id: remote.getCurrentWindow().id,
        data: {
            method: "getVessel",
            args
        }
    })
})